package com.eascs.admin.o2o.detail.controller;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.utils.ComConstants;
import com.eascs.admin.o2o.detail.form.LoanDetailButtonForm;
import com.eascs.admin.o2o.detail.service.LoanDetailButtonService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.entity.O2oSDEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.loan.service.O2oSdService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.DateUtils;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.common.entity.CustomerBasicInfoEntity;
import com.eascs.crm.common.service.ICustomerInfoService;
import com.eascs.crm.common.vo.CustomerView;
import com.eascs.crm.common.vo.QueryCustomerVO;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.workflow.form.SDForm;
import com.eascs.workflow.form.ZXPTCWForm;

@RestController
public class LoanDetailButtonRestController {
	private final Logger logger = LoggerFactory.getLogger(LoanDetailMasterViewController.class);
	@Inject
	private LoanDetailButtonService loanDetailButtonService;

	@Autowired
	private O2oLoanapplyService loanService;

	@Autowired
	private O2oSdService o2oSdService;

	@Reference
	ICustomerInfoService iCustomerInfoService;

	@Value("${sd.url}")
	private String sdUrl;

	@RequestMapping(value = "/detail/loan-detail-button", method = RequestMethod.GET)
	public HtmlRestResult loanDetailButton(LoanDetailButtonForm loanDetailButtonForm) {
		List<Button> buttons = loanDetailButtonService.buildButtons(loanDetailButtonForm);
		ViewContext.put("buttons", buttons);
		return new HtmlRestResult("detail/loanDetailButton");
	}

	@RequestMapping(value = "/getSdData", method = RequestMethod.GET)
	public HtmlRestResult getSdInfo(@RequestParam("customerSd") String customerSd, @RequestParam("cno") String cno) {
		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM");
		String data = "<requestxml><customerCode>" + customerSd + "</customerCode><cno>" + cno + "</cno><beginDate>" + dateFormater.format(new Date()) + "</beginDate></requestxml>";
		List<Object[]> listData = new ArrayList<>();
		String[] titles = { "NO.", "年月", "提货金额（元）", "回款金额（元）", "应收账款（元）", "同步时间" };
		try {
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(sdUrl);
			call.setOperationName("getSDStoreSalesAndReceivedData");
			call.setTimeout(20000);
			String s = (String) call.invoke(new String[] { data });
			logger.info("resp:", s);
			Document doc = DocumentHelper.parseText(s);
			Element root = doc.getRootElement().element("statisticsList");
			if (root != null) {
				@SuppressWarnings("unchecked")
				List<Element> list = root.elements();
				int count = 0;
				SimpleDateFormat date1 = new SimpleDateFormat("yyyy-MM-dd");
				DecimalFormat decimalFormat = null;
				for (Element e : list) {
					decimalFormat = new DecimalFormat(",##0.00");
					double salesMoney = Double.valueOf(String.valueOf(e.element("salesMoney").getData() == null ? 0l : e.element("salesMoney").getData()));
					double receivedMoney = Double.valueOf(String.valueOf(e.element("receivedMoney").getData() == null ? 0l : e.element("receivedMoney").getData()));
					double yingshou = salesMoney - receivedMoney;
					Object[] b = new Object[] { ++count, e.element("statisticDate").getData(), decimalFormat.format(salesMoney), decimalFormat.format(receivedMoney), decimalFormat.format(yingshou), date1.format(new Date()) };
					listData.add(b);
				}
			} else {
				listData = null;
			}

		} catch (Exception e) {
			listData = null;
			ViewContext.put("sdInfo", "fail");
			return HtmlRestResult.html("detail/sd");
		}
		if (listData != null) {
			PaginationInfo paginationInfo = new PaginationInfo(listData, titles);
			paginationInfo.setBuildPagingNavigation(false);
			paginationInfo.setFirstRenderColumnIndex(0);
			ViewContext.put("sdData", paginationInfo);
		} else {
			ViewContext.put("sdInfo", "empty");
		}
		return HtmlRestResult.html("detail/sd");
	}

	@RequestMapping(value = "/getSdDataByName", method = RequestMethod.GET)
	public BaseRestResult<List<Object[]>> getSdInfoByName(@RequestParam("name") String name, @RequestParam("loanType") String loanType) {
		String data = "<requestxml><name>" + name + "</name>" + "</requestxml>";
		List<Object[]> listData = new ArrayList<>();
//		long beginTime = System.currentTimeMillis();
		// 如果是品牌贷 利用 本地数据
		if (StatusContexts.LOAN_TYPE_BP.equals(loanType) || StatusContexts.LOAN_TYPE_BP_JX.equals(loanType) || StatusContexts.LOAN_TYPE_SG.equals(loanType) || StatusContexts.LOAN_TYPE_YC.equals(loanType) || StatusContexts.LOAN_TYPE_SC.equals(loanType)) {
			listData = o2oSdService.getCustomerDataSouce(name);
		} else {
			// 终端贷 数据 门店信息调用sd数据获取数据
			if (name == null || name.length() < 4) {
				return BaseRestResult.buildRestResult(listData);
			}
			try {
				Service service = new Service();
				Call call = (Call) service.createCall();
				call.setTargetEndpointAddress(sdUrl);
				call.setOperationName("getSDCustomerO2OData");
				call.setTimeout(20000);
				String s = (String) call.invoke(new String[] { data });
				// System.out.println(System.currentTimeMillis() - beginTime);
				Document doc = DocumentHelper.parseText(s);
				Element root = doc.getRootElement().element("sdcustomerdata");
				if (root != null) {
					@SuppressWarnings("unchecked")
					List<Element> list = root.elements();
					// for (Element e : list) {
					Element e = null;
					int bigsize = 10;
					if (list.size() < 10) {
						bigsize = list.size();
					}
					// 只取十条防止过慢
					for (int i = 0; i < bigsize; i++) {
						e = list.get(i);
						String cno = String.valueOf(e.element("cno").getData() == null ? 0l : e.element("cno").getData());
						String ccode = String.valueOf(e.element("ccode").getData() == null ? 0l : e.element("ccode").getData());
						String cname = String.valueOf(e.element("cname").getData() == null ? 0l : e.element("cname").getData());
						String cnames = String.valueOf(e.element("cnames").getData() == null ? 0l : e.element("cnames").getData());
						Object[] b = new Object[] { cno + "," + cname + "," + ccode + "," + cnames, cname, ccode, cnames };
						listData.add(b);
					}
				} else {
					listData = null;
				}

			} catch (Exception e) {
				listData = null;
				ViewContext.put("sdInfo", "fail");
				logger.error("拉取sd数据出错，错误信息为:", e);
				// return BaseRestResult.error("拉取sd数据出错:");
				BaseRestResult<List<Object[]>> s = BaseRestResult.buildRestResult(listData);
				s.setAction(BaseRestResult.ACTION_ERROR);
				return s;
			}
		}
		// System.out.println(System.currentTimeMillis() -
		// beginTime+"--------------------");
		return BaseRestResult.buildRestResult(listData);
	}

	/**
	 * 
	 * @param loanapplyNo
	 * @return
	 */
	@RequestMapping(value = "/search_sd")
	public HtmlRestResult toAddSponsor(String customerSdName, String loanType) {
		List<Object[]> listData = new ArrayList<>();
		String[] titles = { "SD号", "简称", "全称" };

		String data = "<requestxml><name>" + customerSdName + "</name>" + "</requestxml>";
		long beginTime = System.currentTimeMillis();
		/*
		 * if(name==null || name.length()<4){ return
		 * HtmlRestResult.info("请输入四位以上的查询"); }
		 */
		// 如果是品牌贷 利用 本地数据
		if (StatusContexts.LOAN_TYPE_BP.equals(loanType) || StatusContexts.LOAN_TYPE_BP_JX.equals(loanType) || StatusContexts.LOAN_TYPE_SG.equals(loanType) || StatusContexts.LOAN_TYPE_YC.equals(loanType) || StatusContexts.LOAN_TYPE_SC.equals(loanType)) {
			listData = o2oSdService.getCustomerDataSouce(customerSdName);
		} else {
			// 终端贷 数据 门店信息调用sd数据获取数据
			try {
				Service service = new Service();
				Call call = (Call) service.createCall();
				call.setTargetEndpointAddress(sdUrl);
				call.setOperationName("getSDCustomerO2OData");
				call.setTimeout(20000);

				String s = (String) call.invoke(new String[] { data });

				System.out.println(System.currentTimeMillis() - beginTime);
				Document doc = DocumentHelper.parseText(s);
				Element root = doc.getRootElement().element("sdcustomerdata");
				if (root != null) {
					@SuppressWarnings("unchecked")
					List<Element> list = root.elements();
					for (Element e : list) {
						String cno = String.valueOf(e.element("cno").getData() == null ? 0l : e.element("cno").getData());
						String ccode = String.valueOf(e.element("ccode").getData() == null ? 0l : e.element("ccode").getData());
						String cname = String.valueOf(e.element("cname").getData() == null ? 0l : e.element("cname").getData());
						String cnames = String.valueOf(e.element("cnames").getData() == null ? 0l : e.element("cnames").getData());
						Object[] b = new Object[] { cno + "," + cname + "," + ccode + "," + cnames, ccode, cname, cnames };
						listData.add(b);
					}
				} else {
					listData = null;
				}
			} catch (Exception e) {
				listData = null;
				ViewContext.put("sdCustomerData", "fail");
				logger.error("拉取sd数据出错，错误信息为:", e);
				// return BaseRestResult.error("拉取sd数据出错:");
				BaseRestResult<List<Object[]>> s = BaseRestResult.buildRestResult(listData);
				s.setAction(BaseRestResult.ACTION_ERROR);
			}
		}
		System.out.println(System.currentTimeMillis() - beginTime + "--------------------");
		if (listData != null) {
			PaginationInfo paginationInfo = new PaginationInfo(listData, titles);
			// paginationInfo.setBuildPagingNavigation(false);
			// paginationInfo.setFirstRenderColumnIndex(0);
			paginationInfo.setBuildSelectBox(true);
			ViewContext.put("sdCustomerData", paginationInfo);
		} else {
			ViewContext.put("sdCustomerData", "empty");
		}
		return HtmlRestResult.popHtmlFragment("daliansd/list");
	}

	/*
	 * //结清确认--批量确认结清
	 * 
	 * @RequestMapping("/upoaando2o") public BaseRestResult<String>
	 * update(String ids, String loansettleddate) { if
	 * (StringUtil.isBlank(loansettleddate)) { return
	 * BaseRestResult.warn("请选择结清日期"); } int j =
	 * loanSettledService.updateLoanStatus(ids, loansettleddate); if (j > 0) {
	 * log.debug("贷款单号为：{}  手动结清成功", ids); } else { log.debug("贷款单号为：{}  手动结清失败"
	 * , ids); } // 记录到操作日志表 String[] id = ids.split(","); O2oLoanapplyEntity
	 * O2oLoanapplyUp = null; O2oLoanapplyEntity O2oLoanapplyDown = null; for
	 * (int i = 0; i < id.length; i++) { ObjectMapper mapper = new
	 * ObjectMapper(); O2oLoanapplyUp = o2oLoanapplyService.findById(id[i]); if
	 * (O2oLoanapplyUp != null) { O2oLoanapplyDown = O2oLoanapplyUp;
	 * O2oLoanapplyDown.setSettleDate(DateUtils.formateStandDate(loansettleddate
	 * )); O2oLoanapplyDown.setWfType(StatusContexts.O2O_REPAY_END); try {
	 * o2oLoanapplyService.saveLog(UserHolder.getUser().getCname(),
	 * O2oLogType.PARTNER_UPDATE_SETTLE,
	 * mapper.writeValueAsString(O2oLoanapplyUp),
	 * mapper.writeValueAsString(O2oLoanapplyDown)); // 记录到操作日志表
	 * o2oLoanapplyService.saveLog(UserHolder.getUser().getCname(),
	 * O2oLogType.LOAN_STATUS_HN, mapper.writeValueAsString(O2oLoanapplyUp),
	 * mapper.writeValueAsString(O2oLoanapplyDown)); // 记录到【结清操作记录】和【作废操作记录】 日志表
	 * o2oRecordLogService.saveLog(O2oLoanapplyDown.getBillCode(),
	 * UserHolder.getUser().getCname(), O2oLogType.LOAN_STATUS_HN,
	 * O2oLoanapplyDown.getSettleDate(), O2oLoanapplyDown.getRemark(),"录入"); }
	 * catch (Exception e) { e.printStackTrace(); } } else { log.debug(
	 * "贷款单号为：{} 此单不存在", id[i]); } } return
	 * BaseRestResult.info("手动结清成功").andRefresh(); }
	 */

	/**
	 * 异步加载 征信表单详情页面编辑征信表单
	 * 
	 * @param zXPTCWForm
	 * @return
	 */
	@RequestMapping(value = "/edit/finance", method = RequestMethod.GET)
	public HtmlRestResult ajaxPtcw(ZXPTCWForm zXPTCWForm) {
		logger.info("征信平台财务接收zXPTCWForm：", zXPTCWForm.toString());
		O2oLoanapplyEntity loanEntity = loanService.findById(zXPTCWForm.getReferId());
		O2oSDEntity sdEntity = o2oSdService.getById(zXPTCWForm.getReferId());
		if (sdEntity == null) {
			QueryCustomerVO vo = new QueryCustomerVO();
			vo.setCustomer_id(loanEntity.getLoanId());
			CustomerView customer = iCustomerInfoService.geCustomerView(vo);
			CustomerBasicInfoEntity custmenBasic = customer.getBasicInfo();
			O2oSDEntity o2osd = querySd(custmenBasic.getCustomerSd(), zXPTCWForm.getReferId(), custmenBasic.getCustomerSdNo());
			ViewContext.put("o2oSd", o2osd);
		} else {
			ViewContext.put("o2oSd", sdEntity);
		}
		O2oLoanapplyEntity loanapplyEntity = loanService.findById(zXPTCWForm.getReferId());
		if (null != loanapplyEntity) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			zXPTCWForm = new ZXPTCWForm();
			zXPTCWForm.setLoanapplyno(loanapplyEntity.getLoanapplyNo());
			zXPTCWForm.setReferId(loanapplyEntity.getLoanapplyNo());
			if (null != loanEntity.getEaCooperationDate())
				zXPTCWForm.setEaTime(format.format(loanapplyEntity.getEaCooperationDate()));
			zXPTCWForm.setEaStockNum(loanapplyEntity.getEaStockNum());
			zXPTCWForm.setTakeAvgAmount(loanapplyEntity.getEaPurchaseamount());
			zXPTCWForm.setArrearsAmountTotal(loanapplyEntity.getEaStockBorrowing());
			zXPTCWForm.setPaymentAvgAmount(loanapplyEntity.getEaReturnedAmount());
			zXPTCWForm.setCustomerSalesamount(loanapplyEntity.getCustomerSalesamount());
			zXPTCWForm.setZxRemark(loanapplyEntity.getZxRemark());
		}
		ViewContext.put("zXPTCWForm", zXPTCWForm);
		return HtmlRestResult.popHtmlFragment("detail/edit_Finance");
	}

	/**
	 * 获取SD数据 插入数据库
	 * 
	 * @param customerSd
	 * @return
	 */
	public O2oSDEntity querySd(String customerSd, String loanapplyNo, String customerSdNo) {
		logger.info("SD数据 插入数据库：", "贷款单号：" + loanapplyNo);
		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM");
		String data = "<requestxml><customerCode>" + customerSd + "</customerCode><cno>" + customerSdNo + "</cno><beginDate>" + dateFormater.format(new Date()) + "</beginDate></requestxml>";
		O2oSDEntity sdEntity = new O2oSDEntity();
		try {
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTimeout(new Integer(15000));
			call.setTargetEndpointAddress(sdUrl);
			call.setOperationName("getSDStoreSalesAndReceivedData");
			String s = (String) call.invoke(new String[] { data });
			logger.info("resp:", s);
			Document doc = DocumentHelper.parseText(s);
			Element root = doc.getRootElement().element("statisticsList");
			if (root != null) {
				@SuppressWarnings("unchecked")
				List<Element> list = root.elements();
				sdEntity.setLoanapplyNo(loanapplyNo);
				sdEntity.setCreateTime(new Date());
				sdEntity.setUpdateTime(new Date());
				for (int i = 0; i < list.size(); i++) {
					if (i == 0) {
						sdEntity.setFirstMonth(String.valueOf(list.get(0).element("statisticDate").getData()));
						sdEntity.setFirstTakeAmount(new BigDecimal(String.valueOf(!StringUtil.isNotBlank(list.get(0).element("salesMoney").getData().toString()) ? "0" : list.get(0).element("salesMoney").getData().toString())));
						sdEntity.setFirstPaymentAmount(new BigDecimal(String.valueOf(!StringUtil.isNotBlank(list.get(0).element("receivedMoney").getData().toString()) ? "0" : list.get(0).element("receivedMoney").getData())));
						sdEntity.setFirstRecAmount(sdEntity.getFirstTakeAmount().subtract(sdEntity.getFirstPaymentAmount()));
					}
					if (i == 1) {
						sdEntity.setSecondMonth(String.valueOf(list.get(1).element("statisticDate").getData()));
						sdEntity.setSecondTakeAmount(new BigDecimal(String.valueOf(!StringUtil.isNotBlank(list.get(1).element("salesMoney").getData().toString()) ? "0" : list.get(1).element("salesMoney").getData())));
						sdEntity.setSecondPaymentAmount(new BigDecimal(String.valueOf(!StringUtil.isNotBlank(list.get(1).element("receivedMoney").getData().toString()) ? "0" : list.get(1).element("receivedMoney").getData())));
						sdEntity.setSecondRecAmount(sdEntity.getSecondTakeAmount().subtract(sdEntity.getSecondPaymentAmount()));
					}
					if (i == 2) {
						sdEntity.setThirdMonth(String.valueOf(list.get(2).element("statisticDate").getData()));
						sdEntity.setThirdTakeAmount(new BigDecimal(String.valueOf(!StringUtil.isNotBlank(list.get(2).element("salesMoney").getData().toString()) ? "0" : list.get(2).element("salesMoney").getData())));
						sdEntity.setThirdPaymentAmount(new BigDecimal(String.valueOf(!StringUtil.isNotBlank(list.get(2).element("receivedMoney").getData().toString()) ? "0" : list.get(2).element("receivedMoney").getData())));
						sdEntity.setThirdRecAmount(sdEntity.getThirdTakeAmount().subtract(sdEntity.getThirdPaymentAmount()));
					}
					if (i == 3) {
						sdEntity.setFourthMonth(String.valueOf(list.get(3).element("statisticDate").getData()));
						sdEntity.setFourthTakeAmount(new BigDecimal(String.valueOf(!StringUtil.isNotBlank(list.get(3).element("salesMoney").getData().toString()) ? "0" : list.get(3).element("salesMoney").getData())));
						sdEntity.setFourthPaymentAmount(new BigDecimal(String.valueOf(!StringUtil.isNotBlank(list.get(3).element("receivedMoney").getData().toString()) ? "0" : list.get(3).element("receivedMoney").getData())));
						sdEntity.setFourthRecAmount(sdEntity.getFourthTakeAmount().subtract(sdEntity.getFourthPaymentAmount()));
					}
					if (i == 4) {
						sdEntity.setFifthMonth(String.valueOf(list.get(4).element("statisticDate").getData()));
						sdEntity.setFifthTakeAmount(new BigDecimal(String.valueOf(!StringUtil.isNotBlank(list.get(4).element("salesMoney").getData().toString()) ? "0" : list.get(4).element("salesMoney").getData())));
						sdEntity.setFifthPaymentAmount(new BigDecimal(String.valueOf(!StringUtil.isNotBlank(list.get(4).element("receivedMoney").getData().toString()) ? "0" : list.get(4).element("receivedMoney").getData())));
						sdEntity.setFifthRecAmount(sdEntity.getFifthTakeAmount().subtract(sdEntity.getFifthPaymentAmount()));
					}
					if (i == 5) {
						sdEntity.setSixthMonth(String.valueOf(list.get(5).element("statisticDate").getData()));
						sdEntity.setSixthTakeAmount(new BigDecimal(String.valueOf(!StringUtil.isNotBlank(list.get(5).element("salesMoney").getData().toString()) ? "0" : list.get(5).element("salesMoney").getData())));
						sdEntity.setSixthPaymentAmount(new BigDecimal(String.valueOf(!StringUtil.isNotBlank(list.get(5).element("receivedMoney").getData().toString()) ? "0" : list.get(5).element("receivedMoney").getData())));
						sdEntity.setSixRecAmount(sdEntity.getSixthTakeAmount().subtract(sdEntity.getSixthPaymentAmount()));
					}
					if (i == 6) {
						sdEntity.setTakeAvgAmount(new BigDecimal(String.valueOf(!StringUtil.isNotBlank(list.get(6).element("salesMoney").getData().toString()) ? "0" : list.get(6).element("salesMoney").getData().toString())));
						sdEntity.setPaymentAvgAmount(new BigDecimal(String.valueOf(!StringUtil.isNotBlank(list.get(6).element("receivedMoney").getData().toString()) ? "0" : list.get(6).element("receivedMoney").getData().toString())));
						sdEntity.setRecAvgAmount(sdEntity.getTakeAvgAmount().subtract(sdEntity.getPaymentAvgAmount()));
					}
				}
			} else {
				sdEntity.setCreateTime(new Date());
				sdEntity.setUpdateTime(new Date());
				sdEntity.setLoanapplyNo(loanapplyNo);
				sdEntity = initEmptySDEntity(sdEntity);// 构建为0的sdEntity对象
			}
			o2oSdService.saveSDEntity(sdEntity);
			if (sdEntity.getPaymentAvgAmount() != null || sdEntity.getTakeAvgAmount() != null) {
				SDForm sdForm = new SDForm();
				sdForm.setLoanapplyNo(loanapplyNo);
				sdForm.setPaymentAvgAmount(sdEntity.getPaymentAvgAmount());
				sdForm.setTakeAvgAmount(sdEntity.getTakeAvgAmount());
				loanService.updatePTCW(sdForm);
			}
		} catch (Exception e) {
			sdEntity = initEmptySDEntity(sdEntity);// 构建为0的sdEntity对象
			o2oSdService.saveSDEntity(sdEntity);
			if (sdEntity.getPaymentAvgAmount() != null || sdEntity.getTakeAvgAmount() != null) {
				SDForm sdForm = new SDForm();
				sdForm.setLoanapplyNo(loanapplyNo);
				sdForm.setPaymentAvgAmount(sdEntity.getPaymentAvgAmount());
				sdForm.setTakeAvgAmount(sdEntity.getTakeAvgAmount());
				loanService.updatePTCW(sdForm);
			}
		}
		return sdEntity;
	}

	public O2oSDEntity initEmptySDEntity(O2oSDEntity sdEntity) {
		for (int i = 0; i < ComConstants.SD_MONTHSIZE; i++) {
			Date date = DateUtils.add(new Date(), Calendar.MONTH, -i);
			String dateString = DateUtils.formateStandTime(date);
			dateString = dateString.substring(0, 7);
			if (i == 0) {
				sdEntity.setFirstMonth(dateString);
				sdEntity.setFirstTakeAmount(new BigDecimal(0));
				sdEntity.setFirstPaymentAmount(new BigDecimal(0));
				sdEntity.setFirstRecAmount(new BigDecimal(0));
			}
			if (i == 1) {
				sdEntity.setSecondMonth(dateString);
				sdEntity.setSecondTakeAmount(new BigDecimal(0));
				sdEntity.setSecondPaymentAmount(new BigDecimal(0));
				sdEntity.setSecondRecAmount(new BigDecimal(0));
			}
			if (i == 2) {
				sdEntity.setThirdMonth(dateString);
				sdEntity.setThirdTakeAmount(new BigDecimal(0));
				sdEntity.setThirdPaymentAmount(new BigDecimal(0));
				sdEntity.setThirdRecAmount(new BigDecimal(0));
			}
			if (i == 3) {
				sdEntity.setFourthMonth(dateString);
				sdEntity.setFourthTakeAmount(new BigDecimal(0));
				sdEntity.setFourthPaymentAmount(new BigDecimal(0));
				sdEntity.setFourthRecAmount(new BigDecimal(0));
			}
			if (i == 4) {
				sdEntity.setFifthMonth(dateString);
				sdEntity.setFifthTakeAmount(new BigDecimal(0));
				sdEntity.setFifthPaymentAmount(new BigDecimal(0));
				sdEntity.setFifthRecAmount(new BigDecimal(0));
			}
			if (i == 5) {
				sdEntity.setSixthMonth(dateString);
				sdEntity.setSixthTakeAmount(new BigDecimal(0));
				sdEntity.setSixthPaymentAmount(new BigDecimal(0));
				sdEntity.setSixRecAmount(new BigDecimal(0));
			}
			if (i == 6) {
				sdEntity.setTakeAvgAmount(new BigDecimal(0));
				sdEntity.setPaymentAvgAmount(new BigDecimal(0));
				sdEntity.setRecAvgAmount(new BigDecimal(0));
			}
		}
		return sdEntity;
	}

}
